const express = require('express');
const cors = require('cors');
const userRouter = require('./router/user')
const userinfoRouter = require('./router/userinfo')
//导入文章分类路由
const artCateRouter = require('./router/artcate')
const app = express();
app.use(express.static('./view'))
app.use(cors());
app.use(express.urlencoded({ extended: false }))
//
app.use((req, res, next) => {
    res.cc = (err, status = 1) => {
        res.send({
            status,
            message: err instanceof Error ? err.message : err
        })
    }
    next();
})
//配置解析token的中间件

const expressJWT = require('express-jwt');//5.3.3 要指定版本下载
const jwtSecretKey = 'itheima No1. ^_^';//加密或解密-密钥
app.use(expressJWT({ secret: jwtSecretKey }).unless({ path: [/^\/api\//] }));
app.use('/api', userRouter)
app.use('/my', userinfoRouter)
//注册文章分类路由
app.use('/my/article', artCateRouter);
// 导入并使用文章路由模块
const articleRouter = require('./router/article')
// 为文章的路由挂载统一的访问前缀 /my/article
app.use('/my/article', articleRouter)
app.use((err, req, res, next) => {
    //定义拦截身份过期或不可用错误
    if (err.name == 'UnauthorizedError') return res.cc('身份认证失败！');
    // 未知错误
    res.cc(err);
})
app.listen(89, () => {
    console.log('http://127.0.0.1:89/login.html');
})